package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Resource
    private CategoryDao categoryDao;

    @Resource
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //1.查出一级分类
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("parent_cid", 0);
        wrapper.eq("cat_level", 1);
        List<CategoryEntity> categoryEntityList = categoryDao.selectList(wrapper);
        if (categoryEntityList != null) {
            //2.查出二级分类
            for (CategoryEntity categoryEntity : categoryEntityList) {
                Long catId = categoryEntity.getCatId();
                wrapper.clear();
                wrapper.eq("parent_cid", catId);
                wrapper.eq("cat_level", 2);
                List<CategoryEntity> categoryEntities = categoryDao.selectList(wrapper);
                categoryEntity.setChildren(categoryEntities);
                if (categoryEntities != null) {
                    //查出三级分类
                    for (CategoryEntity categoryEntity1 : categoryEntities) {
                        Long catId1 = categoryEntity1.getCatId();
                        wrapper.clear();
                        wrapper.eq("parent_cid", catId1);
                        wrapper.eq("cat_level", 3);
                        List<CategoryEntity> categoryEntityList1 = categoryDao.selectList(wrapper);
                        categoryEntity1.setChildren(categoryEntityList1);
                    }
                }
            }
        }
        return categoryEntityList;
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId, paths);
        //将顺序倒置
        Collections.reverse(parentPath);

        return parentPath.toArray(new Long[parentPath.size()]);
    }

    @Override
    @Transactional
    public void updateDetail(CategoryEntity category) {
        baseMapper.updateById(category);

        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogId(category.getCatId());
        categoryBrandRelationEntity.setCatelogName(category.getName());
        categoryBrandRelationDao.update(categoryBrandRelationEntity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id", category.getCatId()));
    }

    private List<Long> findParentPath(Long catelogId, List<Long> paths) {
        paths.add(catelogId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        if (categoryEntity.getParentCid() != 0) {
            findParentPath(categoryEntity.getParentCid(), paths);
        }
        return paths;
    }
}